//
//  ComposeToolbar.swift
//  sinaDemo
//
//  Created by mac on 15/12/24.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

@objc protocol ComposeToolbarDelegate: NSObjectProtocol {
    optional func composeToolbarClickPictureButton()
    optional func composeToolbarClickEmoticonButton()
}

class ComposeToolbar: UIToolbar {
    
    // 点击图片按钮
    @objc private func selectPicture() {
        delegater?.composeToolbarClickPictureButton?()
    }
    
    @objc private func selectEmoticon() {
        delegater?.composeToolbarClickEmoticonButton?()
    }
    
    // 代理属性
    weak var delegater: ComposeToolbarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化子控件
        setUpUI()
    }
    
    // 添加内部按钮
    private func setUpUI() {
        let dictArr = [["imageName": "compose_toolbar_picture","actionName": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "actionName": "selectEmoticon"],
            ["imageName": "compose_addbutton_background"]]

        var toolItems = [UIBarButtonItem]()
        for dict in dictArr {
            let button = UIButton(image: nil, backImage: dict["imageName"]!)
            let barItem = UIBarButtonItem(customView: button)
            toolItems.append(barItem)
            button.addTarget(self, action: Selector(dict["actionName"] ?? ""), forControlEvents: UIControlEvents.TouchUpInside)
            toolItems.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        
        // 清除最后一个弹簧
        toolItems.removeLast()
        items = toolItems
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
